#include <stdio.h>
#include <string.h>
#include <sys/time.h>

#define SPACE 's'
#define RED 'r'
#define BLUE 'b'
#define GREEN 'g'

// Flag vector to signal the end of the wins in the wins array
#define END_OF_WINS 0	

int columns;
int rows;
// NOTE: last_move starts at 1, not 0. This happens to be beneficial because, with padding, the first column is board[1]
int last_move;
int total_time;
int player_1_time;
int last_move_time;

// The Wins Lookup Table
char WINS_LOOKUP[12][4] = {"ggrr","grgr","rgrg","rrgg","grrg","rggr","ggbb","gbgb","bgbg","bbgg","gbbg","bggb"};


/*takes wins as a n by 4 2D array and looks at the different wins and evaluates which player won
and their point values

This function has to do 3 things:
1) figure out which player won - return positive or negative values
2) Figure out what point value they won with - arrangment of the peices
3) If two players have the same winning value - return 1 point
*/
int evalWin(char wins[150][4])
{

	int i = 0;
	int points = 0;
	int bestwin = 0;
	int worstwin = 0;
	int redpoints = 0;
	int bluepoints = 0;
	do{
		//printf("checking case %s\n", wins[i]);
		//check for tie - if there is nothing in the wins array
		if(wins[0][0] == '\0' || wins[0][0] == 0)
		{
			//printf("0");//no wins in array - no nothing in array
			points = 0;
			return 0;
		}
		else//we have at least one win - check it out
		{
		//first - check for the good wins - where the first two char's will match
			if(wins[i][0] == wins[i][1])//same colour first two pieces
			{

				if(wins[i][0] == 'r')
					{ 
						redpoints += 5;
						points = -5;
					}//we won
				else if(wins[i][0] == 'b')
					{
						bluepoints += 5;
						points = 5;
					}
				
				else if(wins[i][0] == 'g')//don't know who won yet
				{
					if(wins[i][2] == 'r')
					{ 
						redpoints += 5;
						points = -5;
					}//we won
					else 
					{
						bluepoints += 5;
						points = 5;
					}
				}
			}
			
			else if(wins[i][1] == wins[i][2])//middle two are same colour - 4 points
			{
				
				if(wins[i][0] == 'r')
				{ 
					redpoints += 4;
					points = -4;
				}//we won
				else if(wins[i][0] == 'b') 
				{
					bluepoints += 4;
					points = 4;
				}
				else//first piece is green
				{//check the second piece
					if(wins[i][1] == 'r')
					{ 
						redpoints += 4;
						points = -4;
					}//we won	
					else if(wins[i][1] == 'b') 
					{
						bluepoints += 4;
						points = 4;
					}
						
				}
					
			}
			else //3 points will be awarded
			{
				if(wins[i][0] == 'r')
				{ 
					redpoints += 3;
					points = -3;
				}//we won
				else if(wins[i][0] == 'b') 
				{
					bluepoints += 3;
					points = 3;
				}
				else//first piece is green
				{//check the second piece
					if(wins[i][1] == 'r')
					{ 
						redpoints += 3;
						points = -3;
					}//we won
					else if(wins[i][1] == 'b')  
					{
						bluepoints += 3;
						points = 3;
					}
				}
			}
			
			
			
		}
		if(points > 0)
		{
			if(bestwin < points)
				bestwin = points;
		}
		else
			if(worstwin > points)
				worstwin = points;

		i++;
	}while(wins[i][0] != 0);

	
	if( redpoints - bluepoints > 0)
		points = worstwin;
	else if(redpoints - bluepoints == 0)
		points = 1;
	else
		points = bestwin;
	
	//printf( "%d\n", points);
	return points;
}

// (7,6,3,600,290,20,r,b,s,s,s,s,b,b,r,g,s,s,b,r,b,b,g,s,r,g,r,b,s,s,r,r,b,r,r,s,s,s,s,s,s,s,s,s,s,s,s,s)
void readBoard(char board[columns+2][rows+2])
{
	int i, j;

	// Populate board array
	for (i = 0; i < columns+2; i++)
	{
		// Padding columns on left and right sides of board
		if (i == 0 || i == columns+1)
		{
			for(j = 0; j < rows+2; j++)
				board[i][j] = SPACE;
			continue;
		}
		// Padding Row over bottom of board
		board[i][0] = SPACE;
		for (j = 1; j < rows+1; j++)
		{
			scanf(",%c", &board[i][j]);
		}
		// Padding row over top of board
		board[i][j] = SPACE;
	}
}

void findWins(char board[columns+2][rows+2], char wins[150][4])
{
	// last_piece_x and last_piece_y hold co-ordinates of the last played piece, that is the piece that must be checked for wins
	int last_piece_x = last_move, last_piece_y, winIndex = 0;
	int i;
	// get the last piece's Y co-ordinate
	for (i=1;i<rows+1;i++)
	{
		if(board[last_piece_x][i] == SPACE)
			break;
		 last_piece_y = i;
	}
	
	//printf("Last Moved Piece's co-ordinates:%i, %i, %c\n", last_piece_x, last_piece_y, board[last_piece_x][last_piece_y]);	
		
	/*
	 * How this works is: For each directional axis (X, Y, Diagonal Down Left-to-Right, and Diagonal Up Left-to-Right), the last dropped
	 * piece can be in 4 vectors (it can be in positions 0-3). For each of these possible positions, the algorithm grabs the 4 pieces
	 * and then checks that vector to see if it is a win. If so, it adds it to the wins array.
	*/
	
	// Diagonal Top Left<->Bottom Right
	//printf("Beginning Diagonal Loop\n");
	for(i=0;i<4;i++) // Check each of the 4 possible positions for the last dropped piece
	{
		short int offset, size = 1;

		//printf("for position:%i\n", i);

		// The co-ordinates used to build the vector to check on. Note these move diagonally to the right,down.
		short int start_piece_x = last_piece_x + i, start_piece_y = last_piece_y - i;
		//printf("start_piece co-ordinates are: %i, %i\n", start_piece_x, start_piece_y);
		// If the starting piece is a space, stop moving in that direction
		if (board[start_piece_x][start_piece_y] == SPACE)
		{
			//printf("BREAK1: Found Space, finished looking at diagonal!\n", i);
			break;
		}

		char win[4] = {board[start_piece_x][start_piece_y],'s','s','s'};
		// Build a vector going diagonally to the up, left
		for (offset = 1;offset < 4;offset++)
		{
			if (board[start_piece_x-offset][start_piece_y+offset] == SPACE)
			{
				//printf("BREAK2: Found Space While building a vector\n");
				break;
			}	
			win[size++] = board[start_piece_x-offset][start_piece_y+offset];
		}
		if (size == 4) // Found a four piece vector (No Spaces)
		{
			//printf("Successfully Found a Four-Piece Vector: %s\n", win);
			int k;
			for (k=0;k<12;k++)
			{
				if (strncmp(win,WINS_LOOKUP[k], 4) == 0)
				{
					//printf("Found a Win! Vector is %s\n", win);
					strcpy(wins[winIndex++], win);
				}
			}
			
		}
	}
	// Top<->Bottom
	//printf("Beginning Vertical Loop\n");
	for(i=0;i<4;i++)
	{
		short int offset, size = 1;
		//printf("for position:%i\n", i);
		short int start_piece_x = last_piece_x, start_piece_y = last_piece_y - i;
		//printf("start_piece co-ordinates are: %i, %i\n", start_piece_x, start_piece_y);
		if (board[start_piece_x][start_piece_y] == SPACE)
		{
			//printf("BREAK1: Found Space, finished looking at Vertical!\n", i);
			break;
		}

		char win[4] = {board[start_piece_x][start_piece_y],'s','s','s'};
		// Build a vector going diagonally to the up, left
		for (offset = 1;offset < 4;offset++)
		{
			if (board[start_piece_x][start_piece_y+offset] == SPACE)
			{
				//printf("BREAK2: Found Space While building a vector\n");
				break;
			}
			win[size++] = board[start_piece_x][start_piece_y+offset];
		}
		if (size == 4) // Found a four piece vector (No Spaces)
		{
			//printf("Successfully Found a Four-Piece Vector: %s\n", win);
			int k;
			for (k=0;k<12;k++)
			{
				if (strncmp(win,WINS_LOOKUP[k], 4) == 0)
				{
					//printf("Found a Win! Vector is %s\n", win);
					strcpy(wins[winIndex++], win);
				}
			}
			
		}
	}
	// Diagonal Bottom Left<->Top Right
	//printf("Beginning 2nd Diagonal Loop\n");
	for(i=0;i<4;i++)
	{
		short int offset, size = 1;
		//printf("for position:%i\n", i);

		short int start_piece_x = last_piece_x - i, start_piece_y = last_piece_y - i;
		//printf("start_piece co-ordinates are: %i, %i\n", start_piece_x, start_piece_y);
		if (board[start_piece_x][start_piece_y] == SPACE)
		{
			//printf("BREAK1: Found Space, finished looking at 2nd diagonal!\n", i);
			break;
		}

		char win[4] = {board[start_piece_x][start_piece_y],'s','s','s'};
		// Build a vector going diagonally to the up, left
		for (offset = 1;offset < 4;offset++)
		{
			if (board[start_piece_x+offset][start_piece_y+offset] == SPACE)
			{
				//printf("BREAK2: Found Space While building a vector\n");
				break;
			}
				
			win[size++] = board[start_piece_x+offset][start_piece_y+offset];
		}
		if (size == 4) // Found a four piece vector (No Spaces)
		{
			//printf("Successfully Found a Four-Piece Vector: %s\n", win);
			int k;
			for (k=0;k<12;k++)
			{
				if (strncmp(win,WINS_LOOKUP[k], 4) == 0)
				{
					//printf("Found a Win! Vector is %s\n", win);
					strcpy(wins[winIndex++], win);
				}
			}
			
		}
	}
	//  Left<->Right
	//printf("Beginning Horizontal Loop\n");
	for(i=0;i<4;i++)
	{
		short int offset, size = 1;
		//printf("for position:%i\n", i);

		short int start_piece_x = last_piece_x - i, start_piece_y = last_piece_y;
		//printf("start_piece co-ordinates are: %i, %i\n", start_piece_x, start_piece_y);
		if (board[start_piece_x][start_piece_y] == SPACE)
		{
			//printf("BREAK1: Found Space, finished looking at Horizontal!\n", i);
			break;
		}

		char win[4] = {board[start_piece_x][start_piece_y],'s','s','s'};
		// Build a vector going diagonally to the up, left
		for (offset = 1;offset < 4;offset++)
		{
			if (board[start_piece_x+offset][start_piece_y] == SPACE)
			{
				//printf("BREAK2: Found Space While building a vector\n");
				break;
			}
				
			win[size++] = board[start_piece_x+offset][start_piece_y];
		}
		if (size == 4) // Found a four piece vector (No Spaces)
		{
			//printf("Successfully Found a Four-Piece Vector: %s\n", win);
			int k;
			for (k=0;k<12;k++)
			{
				if (strncmp(win,WINS_LOOKUP[k], 4) == 0)
				{
					//printf("Found a Win! Vector is %s\n", win);
					strcpy(wins[winIndex++], win);
				}
			}
			
		}
	}
	// End with "end of wins" flag vector, to signal the end of wins
	wins[winIndex][0] = END_OF_WINS;
}

// Print Board function for debugging
void printBoard(char board[columns+2][rows+2])
{
	int i, j;
	// TEST: Print out board
	for (i = 0; i < columns+2; i++)
	{
		for (j = 0; j < rows+2; j++)
		{
			printf("%c ", board[i][j]);
		}
		printf("\n");
	}
	printf("\n");
}

int isWin()
{
    //printf("opened");
	scanf("(%d,%d,%d,%d,%d,%d", &columns, &rows, &last_move, &total_time, &player_1_time, &last_move_time);
	char board[columns+2][rows+2];
	// How many possible simultaneous wins can there be?
	char wins[150][4];
    
	readBoard(board);
	//printBoard(board);
	findWins(board, wins);
	return evalWin(wins);
    
}

int main( int argc, char **argv)
{
	printf("%i\n", isWin());
	return 0;
}

